//
//  ForgetVC.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/8/13.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit

class ForgetVC: BaseVC {
    
    @IBOutlet weak var textButton: UIButton!
    @IBOutlet weak var phoneTF: HDUITextField!
    @IBOutlet weak var passwordTF: HDUITextField!
    @IBOutlet weak var messageTF: HDUITextField!
    @IBOutlet weak var submitButton: TransitionButton!
    
    var ifSendMessage = false
    var count = 60
    var timer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        timer?.invalidate()
    }
    
    @IBAction func textMessage(_ button: UIButton!) {
        print("interesting")
        guard phoneTF.text!.characters.count == 11 else {
            phoneTF.animateLineView()
            return
        }
        if ifSendMessage == false {
            helper.sendMessage(with: phoneTF.text!, completionHandler: { (success) in
                if success {
                    self.textButton.isUserInteractionEnabled = false
                    self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateLabel), userInfo: nil, repeats: true)
                } else {
                    self.showError(message: "发生错误，请重试")
                }
            })
        }
    }
    @objc func updateLabel() {
        if ifSendMessage == false && count > 0 {
            let buttonTitle = "\(count)s"
            print(buttonTitle)
            textButton.setTitle(buttonTitle, for: .normal)
            count -= 1
        } else {
            timer?.invalidate()
            textButton.setTitle("获取验证码", for: .normal)
            textButton.isUserInteractionEnabled = true
            count = 60
        }
    }
    
    @IBAction func submit(_ button: UIButton!) {
        view.endEditing(true)
        guard phoneTF.text != "", phoneTF.text!.characters.count == 11 else {
            phoneTF.animateLineView()
            return
        }
        guard passwordTF.text != "", passwordTF.text!.characters.count > 5 else {
            passwordTF.animateLineView()
            return
        }
        guard messageTF.text != "", messageTF.text!.characters.count == 4 else {
            messageTF.animateLineView()
            return
        }
        submitButton.startAnimation()
        helper.forget(with: phoneTF.text!, password: passwordTF.text!, textMessage: messageTF.text!) { (error) in
            guard let error = error else {
                self.submitButton.stopAnimation()
                self.showSuccess(message: "重置密码成功")
                self.navigationController?.popViewController(animated: true)
                //self.dismiss(animated: true, completion: nil)
                return
            }
            switch error {
            case .networkError:
                self.showError(message: "网络错误，请连接互联网")
            case .wrongMessage:
                self.showError(message: "验证码错误，请重试")
            case .userNotExist:
                self.showError(message: "用户不存在，请重试")
            default:
                break
            }
        }
    }
    
    
    
    
}
